#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2 * 1e5;
int help[10] = { 1,0,0,0,1,0,1,0,2,1 };
int a[N];
int n;
typedef pair<int, int>pp;
pp ret[N];
int solve(int num)
{
    if (num == 0) return 1;
    int ans = 0;
    while (num)
    {
        ans += help[num % 10];
        num /= 10;
    }
    return ans;
}
bool compare(pp& a, pp& b)
{
    if (a.first != b.first)
    {
        return a.first < b.first;
    }
    else
    {
        return a.second < b.second;
    }

}
signed main()
{
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++)
    {
        int num = a[i];
        int tem = solve(num);
        ret[i] = make_pair(tem, num);
    }
    sort(ret, ret+n, compare);
    for (int i = 0; i < n; i++)
    {
        cout << ret[i].second << " ";
    }
    return 0;
}